package com.softeem.gulimail.order.service.impl;

import com.rabbitmq.client.Channel;
import com.softeem.gulimail.order.entity.OrderEntity;
import com.softeem.gulimail.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.common.utils.PageUtils;
import com.softeem.common.utils.Query;
import com.softeem.gulimail.order.dao.OrderItemDao;
import com.softeem.gulimail.order.entity.OrderItemEntity;
import com.softeem.gulimail.order.service.OrderItemService;

@RabbitListener(queues = {"directQueue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     *
     * @param message 消息信息，包括消息头，消息体
     * @param OrderReturnReasonEntity 可以直接将消息类型当作参数 是一个泛型
     * @param channel 当前传输的信道
     */
    @RabbitHandler
    public void listenMessage(Message message,
                              OrderReturnReasonEntity OrderReturnReasonEntity,
                              Channel channel){
        //消息体
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties messageProperties = message.getMessageProperties();
        long deliveryTag = messageProperties.getDeliveryTag();
        System.out.println("消息头消息"+messageProperties+"body["+body+"}+message["+message+"]");

        try {
            if(deliveryTag%2==0){
                /**
                 * 1.投递目标  2.是否批量确认
                 */
                channel.basicAck(deliveryTag,false);
                System.out.println("签收了货物"+deliveryTag);
            }else {
                /**
                 * 1.投递目标 2.是否批量拒绝 3.拒绝方式：true：重新入队列  false：直接丢弃
                 *  如果不指定接受方式，没有确认接受和requeue==true是一样的
                 *  此处判断id是否为偶数，因为奇数的会被重新入队，变成偶数，所以全都可以接受到
                 */
                channel.basicNack(deliveryTag,false,false);

                System.out.println("拒绝了货物"+ deliveryTag);
            }
        }catch (Exception e){

        }

    }
    @RabbitHandler
    public void listenMessage(OrderEntity orderEntity){
        System.out.println("orderEntity===="+orderEntity);
    }

}